$(function() {
    var shName = decodeURI(location.search.split("=")[1]);
    // console.log(shName)
    $("#sh-input").val(shName);

    function render(name) {
        $.ajax({
            type: 'get',
            url: '/product/queryProduct',
            dataType: 'json',
            data: {
                proName: name,
                page: 1,
                pageSize: 100
            },
            success: function(res) {
                $(".area-display").html(template("temp", res))
            }
        })
    }
    render(shName);
    //点击按钮进行搜索
    $(".search-btn").click(function() {
            var changeName = $("#sh-input").val();
            //把数据添加到本地存储
            var arr = JSON.parse(localStorage.getItem('key'));
            let index = arr.indexOf(changeName);
            if (changeName.trim() == 0) {
                mui.toast('请输入关键字！！！')
                return;
            }
            if (index > -1) {
                arr.splice(index, 1)
            }
            arr.unshift(changeName);
            if (arr.length > 6) {
                arr.pop();
            }
            localStorage.setItem("key", JSON.stringify(arr));
            render(changeName);
            $("#sh-input").val("");
        })
        //给每个li添加点击事件进行页面跳转
    $(".area-display").on("click", "li", function() {
        var id = $(this).data("id");
        console.log(id)
        location.href = "./product.html?id=" + id;
    })
})